<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="utf-8" lang="utf-8">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>
   查询辅助函数 - CodeIgniter 中文手册|用户手册|用户指南|Wiki文档
  </title>
  <link rel="shortcut icon" href="/user_guide/../images/design/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" type="text/css" media="all" href="/user_guide/userguide.css" />
  <link rel="search" href="/user_guide/../CodeIgniterSearch.xml" type="application/opensearchdescription+xml" title="CodeIgniter 搜索" />
  <link rel="canonical" href="/user_guide/database/helpers.html" />
  <script type="text/javascript" src="/user_guide/nav/nav.js">
  </script>
  <script type="text/javascript" src="/user_guide/nav/prototype.lite.js">
  </script>
  <script type="text/javascript" src="/user_guide/nav/moo.fx.js">
  </script>
  <script type="text/javascript" src="/user_guide/nav/user_guide_menu.js">
  </script>
  <meta name="robots" content="all" />
  <meta name="author" content="ExpressionEngine Dev Team" />
  <meta name="description" content="CodeIgniter 中文手册, CodeIgniter 用户指南, CodeIgniter User Guide, Wiki 文档" />
 </head>
 <body>
  <!-- START NAVIGATION -->
  <div id="nav">
   <div id="nav_inner">
    <script type="text/javascript">
     create_menu('/user_guide/');
    </script>
   </div>
  </div>
  <script type="text/javascript">
   _setNavigation();
  </script>
  <div id="nav2">
   <a name="top">
   </a>
   <a href="javascript:void(0);" onclick="myHeight.toggle();">
    <img src="/user_guide/images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="切换目录" alt="切换目录" />
   </a>
  </div>
  <div id="masthead">
   <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
    <tr>
     <td width="350">
      <h1>
       CodeIgniter 用户指南 版本 2.0.0
      </h1>
     </td>
     
     <td id="breadcrumb_right">
      <a href="/user_guide/toc.html">
       目录页
      </a>
     </td>
    </tr>
   </table>
  </div>
  <!-- END NAVIGATION -->
  <!-- START BREADCRUMB -->
  <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
   <tr>
    <td id="breadcrumb">
     <a href="/" target="_blank">
      CodeIgniter 中国首页
     </a>
     &nbsp;&#8250;&nbsp;
     <a href="/user_guide/toc.html">用户指南目录</a>
     &nbsp;&#8250;&nbsp;
     <a href="/user_guide/database/index.html">
      数据库类
     </a>
     &nbsp;&#8250;&nbsp;查询辅助函数
    </td>
    <td id="searchbox">
     <form method="get" action="http://www.google.com/search" target="google_window">
      <input type="hidden" name="client" value="pub-0176846097796333" />
      <input type="hidden" name="forid" value="1" />
      <input type="hidden" name="ie" value="UTF-8" />
      <input type="hidden" name="oe" value="UTF-8" />
      <input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.org.cn/user_guide/" />
      搜索用户指南&nbsp;
      <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />
      &nbsp;
      <input type="submit" class="submit" name="sa" value="Go" />
     </form>
    </td>
   </tr>
  </table>
  <!-- END BREADCRUMB -->
  <div style="clear:both;text-align:right;padding: 6px 40px 0 0;">
   <a href="http://codeigniter.com/user_guide/database/helpers.html" target="_blank">
    查看原文
   </a>
  </div>
  <!--<br clear="all"  />-->
  <!-- START CONTENT -->
  <div id="content">
   <h1>
    查询辅助函数
   </h1>
   <h2>
    $this-&gt;db-&gt;insert_id()
   </h2>
   <p>
    The insert ID number when performing database inserts.
   </p>
   <br />
   这个ID号是执行数据插入时的ID。
   <h2>
    $this-&gt;db-&gt;affected_rows()
   </h2>
   <p>
    Displays the number of affected rows, when doing "write\" type queries (insert, update, etc.).
   </p>
   <br />
   当执行写入操作（insert,update等）的查询后，显示被影响的行数。
   <br />
   <p>
    Note:  In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database class has a small hack that allows it to return the
correct number of affected rows.  By default this hack is enabled but it can be turned off in the database driver file.
   </p>
   <br />
   注意：在 MySQL 中“DELETE FROM TABLE”的被影响行数将会返回 0。database 类有一个小 hack 允许返回正确的被影响的行数。默认情况下这个 hack 功能是打开的但可以在数据库驱动文件中关闭它。
   <h2>
    $this-&gt;db-&gt;count_all();
   </h2>
   <p>
    Permits you to determine the number of rows in a particular table.  Submit the table name in the first parameter. Example:
   </p>
   <br />
   计算出指定表的总行数并返回。在第一个参数中写入被提交的表名。例如：
   <code>
    echo $this-&gt;db-&gt;count_all('
    <var>
     my_table
    </var>
    ');
    <br />
    <br />
    // Produces an integer, like 25
   </code>
   <h2>
    $this-&gt;db-&gt;platform()
   </h2>
   <p>
    Outputs the database platform you are running (MySQL, MS SQL, Postgres, etc...):
   </p>
   <p>
    输出系统使用的数据库平台(MySQL, MS SQL, Postgres……)
   </p>
   <code>
    echo $this-&gt;db-&gt;platform();
   </code>
   <h2>
    $this-&gt;db-&gt;version()
   </h2>
   <p>
    Outputs the database version you are running:
   </p>
   <br />
   输出系统正在运行的数据库版本号
   <code>
    echo $this-&gt;db-&gt;version();
   </code>
   <h2>
    $this-&gt;db-&gt;last_query();
   </h2>
   <p>
    Returns the last query that was run (the query string, not the result).  Example:
   </p>
   <p>
    返回最后运行的查询（是查询语句，不是查询结果）
   </p>
   <code>
    $str = $this-&gt;db-&gt;last_query();
    <br />
    <br />
    // Produces:  SELECT * FROM sometable....
   </code>
   <p>
    The following two functions help simplify the process of writing database INSERTs and UPDATEs.
   </p>
   <br />
   下面的两个函数简化了写入数据库的insert和update函数。
   <h2>
    $this-&gt;db-&gt;insert_string();
   </h2>
   <p>
    This function simplifies the process of writing database inserts. It returns a correctly formatted SQL insert string. Example:
   </p>
   <br />
   这个函数简化了写入数据库的insert函数。它返回一个标准的SQL insert字符串。例如：
   <code>
    $data = array('name' => $name, 'email' => $email, 'url' => $url);
    <br />
    <br />
    $str = $this-&gt;db-&gt;insert_string('table_name', $data);
   </code>
   <p>
    The first parameter is the table name, the second is an associative array with the data to be inserted.  The above example produces:
   </p>
   <br />
   第一个参数是表名，第二个是被插入数据的联合数组，上面的例子生成的效果为：
   <code>
    INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@example.com', 'example.com')
   </code>
   <p class="important">
    Note: Values are automatically escaped, producing safer queries.
   </p>
   <br />
   注解：被插入的数据会被自动转换和过滤，生成安全的查询语句。
   <h2>
    $this-&gt;db-&gt;update_string();
   </h2>
   <p>
    This function simplifies the process of writing database updates. It returns a correctly formatted SQL update string. Example:
   </p>
   <br />
   这个函数简化了写入数据库的update函数。它返回一个标准的SQL update字符串。例如：
   <code>
    $data = array('name' => $name, 'email' => $email, 'url' => $url);
    <br />
    <br />
    $where = "author_id = 1 AND status = 'active'";
    <br />
    <br />
    $str = $this-&gt;db-&gt;update_string('table_name', $data, $where);
   </code>
   <p>
    The first parameter is the table name, the second is an associative array with the data to be updated, and the third parameter is the "where" clause. The above example produces:
   </p>
   <br />
   第一个参数是表名，第二个是被更新数据的关联数组，第三个参数是“where”参数。上面的例子生成的效果为：
   <code>
    UPDATE table_name SET name = 'Rick', email = 'rick@example.com', url = 'example.com' WHERE author_id = 1 AND status = 'active'
   </code>
   <p class="important">
    Note: Values are automatically escaped, producing safer queries.
   </p>
   <br />
   注解：被插入的数据会被自动转换和过滤，生成安全的查询语句。
   <p>
    &nbsp;
   </p>
   <div id="Contributors">
    翻译贡献者:
analyzer, Hex, IT不倒翁
   </div>
   <div id="DocDate">
    最后修改: 2011-04-21 09:43:01
   </div>
  </div>
  <!-- END CONTENT -->
  <div id="footer">
   <p>
    上一个主题:&nbsp;&nbsp;
    <a href="/user_guide/database/results.html">
     查询结果
    </a>
    &nbsp;&nbsp;&middot;&nbsp;&nbsp;
    <a href="#top">
     页首
    </a>
    &nbsp;&middot;&nbsp;&nbsp;
    <a href="/user_guide/toc.html">用户指南目录</a>
    &nbsp;&nbsp;&middot;&nbsp;&nbsp;下一个主题:&nbsp;&nbsp;
    <a href="/user_guide/database/active_record.html">
     Active Record 模式
    </a>
   </p>
   <p>
    <a href="http://codeigniter.com">
     CodeIgniter
    </a>
    &nbsp;&middot;&nbsp; 版权所有 &#169; 2006-2011 &nbsp;&middot;&nbsp;
    <a href="http://ellislab.com/">
     Ellislab, Inc.
    </a>
   </p>
   <p>
    中文化:
    <a href="">
     CodeIgniter 中国
    </a>
    &nbsp;&middot;&nbsp; 制作: Hex &nbsp;&middot;&nbsp; 版本: 1.20 &nbsp;&middot;&nbsp; 鸣谢: 子非鱼
   </p>
  </div>
  
  <div style="display:none">
   
   <noscript>
    <a href="http://www.51.la/?3289908" target="_blank">
     <img alt="我要啦免费统计" src="http://img.users.51.la/3289908.asp" style="border:none" />
    </a>
   </noscript>
  </div>
 </body>
</html>